package com.tsll.controller;


import com.tsll.annotation.MyLog;
import com.tsll.entity.Staff;
import com.tsll.service.StaffService;
import com.tsll.vo.Result;
import com.tsll.vo.StaffVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;


@RestController
@RequestMapping("/staff")
public class StaffController {


    @Autowired
    private HttpSession session;
    @Autowired
    private StaffService staffServiceImpl;
    @GetMapping("/getInfo")
    public Result getInfo(Staff staffs) {
        //获取当前登录者的用户信息。---session
        Object staff = session.getAttribute("staff");
        List<Staff> by = staffServiceImpl.findBy(staffs);
        return new Result(200, "获取用户信息成功", staff);
    }

    //查询所有员工
    @MyLog(businessName = "staff查询所有员工")
    @PostMapping("/list/{current}/{pageSize}")
    public Result list(@PathVariable Integer current, @PathVariable Integer pageSize, @RequestBody StaffVo staffVo) {
        return staffServiceImpl.findByPage(current, pageSize, staffVo);
    }

    //查询离职员工
    @MyLog(businessName = "staff查询离职员工")
    @PostMapping("/lizhi/{current}/{pageSize}")
    public Result li(@PathVariable Integer current, @PathVariable Integer pageSize, @RequestBody StaffVo staffVo) {
        return staffServiceImpl.lizhi(current, pageSize, staffVo);
    }

    //   查询员工工资
    @MyLog(businessName = "查询staff员工工资")
    @PostMapping("/all/{current}/{pageSize}")
    public Result all(@PathVariable Integer current, @PathVariable Integer pageSize, @RequestBody StaffVo staffVo) {
        Result all = staffServiceImpl.all(current, pageSize, staffVo);
        return all;

    }

    /**
     * 根据指定的ID删除员工信息。
     */
        @MyLog(businessName = "删除staff员工信息")
    @RequestMapping("/delete/{id}")
    public int delete(@PathVariable Integer id) {
        return staffServiceImpl.deleteByPrimaryKey(id);
    }

    @RequestMapping("/deletestaff/{id}")
    public Result deleteall(@PathVariable Integer id) {
        return staffServiceImpl.delete(id);
    }

    /**
     * 添加员工
     *
     * @param staff 员工信息
     */
    @MyLog(businessName = "添加staff员工信息")
    @RequestMapping("/add")
    public Result addStaff(@RequestBody Staff staff) {
        if (staff == null) {
            return new Result(400, "用户信息为空", null);
        }
        staff.setState(1);
        int count = staffServiceImpl.addstaff(staff);
        if (count == 1) {
            return new Result(200, "添加用户信息成功", count);
        }
        return new Result(500, "添加用户信息失败", count);
    }


    /**
     * 编辑员工
     */
    @MyLog(businessName = "编辑staff员工信息")
    @RequestMapping("/update")
    public Result update(@RequestBody Staff staff) {
        if (staff == null) {
            return new Result(400, "用户信息为空", null);
        }
        int update = staffServiceImpl.updatesatff(staff);
        if (update == 1) {
            return new Result(200, "修改用户信息成功", update);
        }
        return new Result(500, "修改用户信息失败", update);
    }


    /**
     * 离职
     */
    @RequestMapping("/updateById/{id}")
    public  Result updateById(@PathVariable Integer id){
        return staffServiceImpl.updateById(id);
    }


    @RequestMapping("/isStaffid/{id}")
    public Result selectByStaffid(@PathVariable Integer id){
        return staffServiceImpl.selectByStaffid(id);
    }

}
